import Core from '@alicloud/pop-core';
import moment from 'moment';

const client = new Core({
  accessKeyId: 'LTAI4G9htmtvy7PZRqar3766',
  accessKeySecret: 'Hhb02iLZxXyFX1XiagZlB9vC2vFuHU',
  endpoint: 'https://cloudpush.aliyuncs.com',
  apiVersion: '2016-08-01',
});

export const sendNotification = ({
  title, body, os, pushid, extend
}: {
  title: string;
  body: string;
  os: 'ios' | 'android';
  pushid: string;
  extend?: object;
}) => {
  console.log('[time] ', moment().add(3, 'day').toISOString());
  let params = {
    RegionId: 'cn-shanghai',
    AppKey: '333378986',
    PushType: 'NOTICE',
    DeviceType: 'ALL',
    Target: 'DEVICE',
    TargetValue: pushid,
    Body: body,
    Title: title,
    ExpireTime: moment().add(3, 'day').format('YYYY-MM-DDTHH:mm:ss') + 'Z',
    AndroidNotificationChannel: 'mtpush',
    AndroidNotificationXiaomiChannel: 'pre84',
    AndroidPopupActivity: 'cc.mengti.workapp.MainActivity',
    AndroidPopupTitle: title,
    AndroidPopupBody: body,
    StoreOffline: 'true'
  };
  let requestOption = {
    method: 'POST',
  };
  client.request('Push', params, requestOption).then(
    (result) => {
      console.log(JSON.stringify(result));
    },
    (ex) => {
      console.log(ex);
    },
  );

};
